﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Data.Common;
using Apq.DB;

namespace Apq.DB.Privilege
{
	/// <summary>
	/// 权限项
	/// </summary>
	public class Common
	{
		#region 列表权限项树
		/// <summary>
		/// 列表权限项树
		/// </summary>
		/// <param name="FuncCode">根权限项</param>
		/// <returns></returns>
		public static System.Data.DataSet Func_List(string FuncCode)
		{
			System.Data.DataSet ds = new System.Data.DataSet();
			DbConnection SqlConn = null;
			using (SqlConn = Apq.DBC.Common.CreateDBConnection("Privilege", ref SqlConn))
			{
				DbConnectionHelper dbch = new DbConnectionHelper(SqlConn);
				DbDataAdapter sda = dbch.CreateAdapter();
				sda.SelectCommand.CommandText = @"
DECLARE @FuncID bigint;
SELECT @FuncID = FuncID FROM dbo.Func WHERE FuncCode = @FuncCode;
SELECT * FROM dbo.Func WHERE ParentID = @FuncID";
				sda.SelectCommand.CommandType = CommandType.StoredProcedure;
				DbCommandHelper dch = new DbCommandHelper(sda.SelectCommand);
				dch.AddParameter("rtn", 0, DbType.Int32);
				dch.AddParameter("PID", FuncCode, DbType.Int64);
				sda.SelectCommand.Parameters["rtn"].Direction = ParameterDirection.ReturnValue;
				SqlConn.Open();
				sda.Fill(ds);
				//stReturn.NReturn = System.Convert.ToInt32(sc.Parameters["rtn"].Value);

				SqlConn.Close();
			}

			return ds;
		}
		#endregion

		#region 编辑
		/// <summary>
		/// 编辑
		/// </summary>
		public static STReturn ApqPrivilege_Edit(long PID, long ParentID, string PName, string Remark)
		{
			STReturn stReturn = new STReturn();
			
			DbConnection SqlConn = null;
			using (SqlConn = Apq.DBC.Common.CreateDBConnection("Privilege", ref SqlConn))
			{
				DbConnectionHelper dbch = new DbConnectionHelper(SqlConn);
				DbCommand sc = SqlConn.CreateCommand();
				sc.CommandText = "dbo.ApqPrivilege_Edit";
				sc.CommandType = CommandType.StoredProcedure;
				Apq.DB.DbCommandHelper dch = new Apq.DB.DbCommandHelper(sc);
				dch.AddParameter("rtn", 0, DbType.Int32);
				dch.AddParameter("PID", PID, DbType.Int64);
				dch.AddParameter("ParentID", ParentID, DbType.Int64);
				dch.AddParameter("PName", PName);
				dch.AddParameter("Remark", Remark);
				sc.Parameters["rtn"].Direction = ParameterDirection.ReturnValue;
				sc.Parameters["PID"].Direction = ParameterDirection.InputOutput;
				sc.Parameters["ParentID"].Direction = ParameterDirection.InputOutput;
				sc.Parameters["PName"].Direction = ParameterDirection.InputOutput;
				sc.Parameters["Remark"].Direction = ParameterDirection.InputOutput;
				SqlConn.Open();
				sc.ExecuteNonQuery();

				stReturn.NReturn = System.Convert.ToInt32(sc.Parameters["rtn"].Value);
				stReturn.POuts.Add(sc.Parameters["PID"].Value);
				stReturn.POuts.Add(sc.Parameters["ParentID"].Value);
				stReturn.POuts.Add(sc.Parameters["PName"].Value);
				stReturn.POuts.Add(sc.Parameters["Remark"].Value);
				stReturn.ExMsg = Apq.Convert.ChangeType<string>(sc.Parameters["ExMsg"].Value);

				SqlConn.Close();
			}

			return stReturn;
		}
		#endregion

		#region 删除
		/// <summary>
		/// 删除
		/// </summary>
		public static STReturn ApqUser_Delete(long PID, long ParentID)
		{
			STReturn stReturn = new STReturn();
			
			DbConnection SqlConn = null;
			using (SqlConn = Apq.DBC.Common.CreateDBConnection("Privilege", ref SqlConn))
			{
				DbConnectionHelper dbch = new DbConnectionHelper(SqlConn);
				DbCommand sc = SqlConn.CreateCommand();
				sc.CommandText = "dbo.ApqUser_Delete";
				sc.CommandType = CommandType.StoredProcedure;
				Apq.DB.DbCommandHelper dch = new Apq.DB.DbCommandHelper(sc);
				dch.AddParameter("rtn", 0, DbType.Int32);
				dch.AddParameter("PID", PID, DbType.Int64);
				dch.AddParameter("ParentID", ParentID, DbType.Int64);
				sc.Parameters["rtn"].Direction = ParameterDirection.ReturnValue;
				SqlConn.Open();
				sc.ExecuteNonQuery();

				stReturn.NReturn = System.Convert.ToInt32(sc.Parameters["rtn"].Value);

				SqlConn.Close();
			}

			return stReturn;
		}
		#endregion
	}
}
